Docker Build
Docker Build
The docker build command is
used to build Docker images from a Dockerfile. Here's the basic syntax:
docker
build [OPTIONS] PATH
docker
build [OPTIONS] URL
URL can be Git
repositories, pre-packaged tarball contexts and plain text files
Example:
docker build http://online/content.tar.gz
docker
build - < Dockerfile
Where OPTIONS are various
options that can be used to customize the build process, and PATH is the
path to the directory containing the Dockerfile.
Some common options that can be
used with the docker build command:
- -t: Set a tag for the image being built. For
example:
docker
build -t my-app .
This command
builds a Docker image from the Dockerfile in the current directory and
tags it with the name my-app.
- --no-cache: Do not use the cache when building
the image. For example:
docker
build --no-cache -t my-app .
This command
builds a Docker image from the Dockerfile in the current directory but
ignores any cached layers from previous builds.
- -f: Specify the path to a different
Dockerfile. For example:
docker
build -f path/to/Dockerfile -t my-app .
This command
builds a Docker image from the Dockerfile located at path/to/Dockerfile,
and tags it with the name my-app.
- --build-arg: Pass build-time variables to the
Dockerfile. For example:
docker
build --build-arg MY_VAR=my_value -t my-app .
This command
builds a Docker image from the Dockerfile in the current directory and
sets the build-time variable MY_VAR to my_value.
- -q: Quiet mode - only output the final image
ID. For example:
docker
build -q -t my-app .
This command
builds a Docker image from the Dockerfile in the current directory and
only outputs the final image ID when the build is complete.
- --pull: Always attempt to pull a newer version
of the base image. For example:
docker
build --pull -t my-app .
This command
builds a Docker image from the Dockerfile in the current directory and
tries to pull a newer version of the base image from the Docker Hub registry
before starting the build.
- --progress: Set the build progress output type
(either auto, plain, or tty). For example:
docker
build --progress=tty -t my-app .
This command
builds a Docker image from the Dockerfile in the current directory and
outputs the build progress information in a tty-friendly format.
8.
--target: Set the target build stage in
the Dockerfile. For example:
docker
build --target my-stage -t my-app .
This command
builds a Docker image from the Dockerfile in the current directory and
only builds the stage named my-stage in the Dockerfile. This can be
useful for speeding up builds by skipping stages that don't need to be rebuilt.
9.
--network: Set the network mode for the
build. For example:
docker
build --network host -t my-app .
This command
builds a Docker image from the Dockerfile in the current directory and
sets the network mode to host, which allows the container to use the
host machine's network stack.
- --shm-size: Set the shared memory size for the
build. For example:
docker
build --shm-size=1g -t my-app .
This command
builds a Docker image from the Dockerfile in the current directory and
sets the shared memory size to 1 gigabyte. This can be useful for applications
that require a larger shared memory segment.
- --ulimit: Set the ulimit options for the
build. For example:
docker
build --ulimit nofile=1024:1024 -t my-app .
This command
builds a Docker image from the Dockerfile in the current directory and
sets the nofile ulimit option to 1024.
- --label: Set metadata labels for the image.
For example:
docker
build --label my-label=value -t my-app .
This command
builds a Docker image from the Dockerfile in the current directory and
sets the label my-label to value.
- --quiet: Do not output any build information,
only the final image ID. For example:
docker
build --quiet -t my-app .
This command
builds a Docker image from the Dockerfile in the current directory and
only outputs the final image ID when the build is complete.
- --platform: Set the target platform for the
build. For example:
docker
build --platform linux/arm64 -t my-app .
This command
builds a Docker image from the Dockerfile in the current directory and
sets the target platform to linux/arm64.
- --secret: Provide a secret file to the build.
For example:
docker
build --secret id=my-secret,src=path/to/secret -t my-app .
This command
builds a Docker image from the Dockerfile in the current directory and
provides a secret file named my-secret located at path/to/secret
to the build context.
- --squash: Squash the layers of the image into
a single layer. For example:
docker
build --squash -t my-app .
This command
builds a Docker image from the Dockerfile in the current directory and
squashes all the image layers into a single layer.
- --file: Specify the name of the Dockerfile to
use for the build. For example:
docker
build --file my-other-Dockerfile -t my-app .
This command
builds a Docker image from the my-other-Dockerfile located in the
current directory and tags it with the name my-app.
- --cache-from: Use an image from a previous
build as the cache source. For example:
docker
build --cache-from my-app:latest -t my-app .
This command
builds a Docker image from the Dockerfile in the current directory and
uses the image my-app:latest from a previous build as the cache source.
- --cpuset-cpus: Set the CPUs to use for the
build process. For example:
docker
build --cpuset-cpus 0-3 -t my-app .
This command
builds a Docker image from the Dockerfile in the current directory and
sets the CPUs used for the build process to 0 through 3.
- --memory: Set the maximum memory for the build
process. For example:
docker
build --memory 4g -t my-app .
This command
builds a Docker image from the Dockerfile in the current directory and
sets the maximum memory for the build process to 4 gigabytes.
In addition to
the basic syntax of the docker build command, build-time variables
(often called build arguments or simply args) can be used in the Dockerfile.
These allow you to pass values to the Dockerfile during the build process,
which can be useful for customizing the build based on different environments
or requirements.
Here's how to
define a build argument in a Dockerfile:
ARG
my_variable=default_value
In this example, my_variable
is the name of the argument, and default_value is the default value that
will be used if the argument is not passed in during the build.
To pass a value
for the argument during the build process, use the --build-arg option
with the docker build command:
docker
build --build-arg my_variable=new_value .
docker
build --build-arg HTTP_PROXY=http://1.2.3.4:1234 --build-arg FTP_PROXY=http://10.20.30.40:5678
.
In this example, my_variable
is the name of the argument, and new_value is the value that will be
passed to the Dockerfile during the build.
Once the build is
complete, you can access the value of the argument in the Dockerfile using the ${my_variable}
syntax. For example, you might use it to set an environment variable or
customize a command:
ENV
MY_ENV_VAR=${my_variable} RUN some_command ${my_variable}
export
HTTP_PROXY=http://10.20.30.2:1234
docker
build --build-arg HTTP_PROXY .
Note that build
arguments are not persisted in the final image. They are only used during the
build process.
By default, the docker
build command creates a local container image as the build result. However,
using the --output (or -o) flag allows you to change this
behavior and specify a custom exporter for the build artifacts. Custom
exporters enable you to export the build results as files on the local file
system instead of a Docker image. This can be helpful in generating local binaries,
code generation, and other use cases.
The --output
flag value is a comma-separated string defining the exporter type and options.
Currently, two exporters are supported: local and tar. The local
exporter writes the build files to a directory on the client side, whereas the tar
exporter writes the files as a single tarball (.tar) file.
Here are a few
examples of how to use the --output option:
- Build and save to a local directory:
docker
build -t my-image --output type=local,dest=out/ .
This command
builds an image with the tag my-image and saves the output to a local
directory named out/.
- Build and push to a remote registry:
docker
build -t my-registry/my-image --output type=image,push=true .
This command
builds an image with the tag my-registry/my-image and pushes the output
to a remote registry.
- Build and export as a tar archive:
docker
build -t my-image --output type=tar,dest=out.tar .
This command
builds an image with the tag my-image and exports the output as a tar
archive to a local file named out.tar.
- Build and save to a remote directory:
docker
build -t my-image --output type=remote,url=https://my-remote-server.com/my-images
.
This command
builds an image with the tag my-image and saves the output to a remote
directory named my-images on a remote server at https://my-remote-server.com/.
- Build and save to a Docker registry with a digest:
docker
build -t my-registry/my-image --output type=image,dest=my-registry/my-image@sha256:<digest>
.
This command
builds an image with the tag my-registry/my-image and saves the output
to a Docker registry with the specified digest. This can be useful when you
want to ensure that the exact same image is used in different environments.
Once the build is
complete, docker run command is used to create a container from
the image.
Comments
Post a Comment